//
//  BU2ViewController.m
//  Balance
//
//  Created by Calvin Kuo on 12-04-29.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "BU2ViewController.h"

@implementation BU2ViewController
@synthesize buNames = _buNames;
@synthesize buTableView = _buTableView;
@synthesize delegate;
@synthesize identifier;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    self.buNames = [NSMutableArray array];
    [_buNames addObject:@"Vision Operations"];
    [_buNames addObject:@"Vision Food"];
    [_buNames addObject:@"Vision Securities"];
    [_buNames addObject:@"Vision A"];
    [_buNames addObject:@"Vision B"];
    [_buNames addObject:@"Vision C"];
    [_buNames addObject:@"Vision D"];
    [_buNames addObject:@"Vision E"];
    [_buNames addObject:@"Vision F"];
    [_buNames addObject:@"Vision G"];
    [_buNames addObject:@"Vision H"];    
    checkAll = NO;
    uncheckAll = NO;
    initial = YES;

}

- (void)viewDidUnload
{
    [self setBuTableView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
	return YES;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return [_buNames count];;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"BU2Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    NSString *bu = [_buNames objectAtIndex:indexPath.row];
    cell.textLabel.text = bu;
    cell.textLabel.font = [UIFont systemFontOfSize:18.0];
    
    if ((!checkAll) && (!uncheckAll)){
        
        
        //default to select first 5 rows. only work when the app is first up.
        if ((initial) && (indexPath.row < 5)){
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            if (indexPath.row == 4) initial = NO;
        }
        else{
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        [self.delegate bu2ViewController:self status:@"Multiple"];
        
        
    } else if ((checkAll) && (!uncheckAll)){
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else if ((checkAll) && (!uncheckAll)){
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    
    // Configure the cell...
    
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
    
    if (newCell.accessoryType == UITableViewCellAccessoryNone) {
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
        [self.delegate bu2ViewController:self status:@"Multiple"];
        
    }else {
        newCell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    BOOL selected = NO;
    for (int i = 0; i < [_buTableView numberOfSections]; i++) {
        for (int j = 0; j < [_buTableView numberOfRowsInSection:i]; j++) {
            NSUInteger ints[2] = {i,j};
            NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:ints length:2];
            UITableViewCell *cell = [_buTableView cellForRowAtIndexPath:indexPath];
            if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
                selected = YES;
            
        }
    }
    if (selected) 
        [self.delegate bu2ViewController:self status:@"Multiple"];
    else 
        [self.delegate bu2ViewController:self status:@"None"];}


- (IBAction)checkAll:(id)sender {
    
    checkAll = YES;
    uncheckAll = NO;
    for (int i = 0; i < [_buTableView numberOfSections]; i++) {
        for (int j = 0; j < [_buTableView numberOfRowsInSection:i]; j++) {
            NSUInteger ints[2] = {i,j};
            NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:ints length:2];
            UITableViewCell *cell = [_buTableView cellForRowAtIndexPath:indexPath];
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            
        }
    }
    
    
    [self.delegate bu2ViewController:self status:@"All"];
}

- (IBAction)unCheckAll:(id)sender {
   
    uncheckAll = YES;
    checkAll = NO;
    for (int i = 0; i < [_buTableView numberOfSections]; i++) {
        for (int j = 0; j < [_buTableView numberOfRowsInSection:i]; j++) {
            NSUInteger ints[2] = {i,j};
            NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:ints length:2];
            UITableViewCell *cell = [_buTableView cellForRowAtIndexPath:indexPath];
            cell.accessoryType = UITableViewCellAccessoryNone;
            
        }
    }
    
    [self.delegate bu2ViewController:self status:@"None"];  
}
@end
